package sessions

import (
	"gitee.com/sdynasty/clever/clog"
	"gitee.com/sdynasty/clever/socket"
)

type Session struct {
	Fd    		uint32
	connWs		*socket.ConnectionWs
	connNet		*socket.ConnectionNet
	userData 	*AgentUserData
}

type AgentUserData struct {
	Uid int32
	Account string
	ClubId int32
	RoomServerAddr string   // 房间服Ip
	RoomServerPtr interface{}
}

func NewSessionWithWS(fd uint32, con *socket.ConnectionWs) *Session {
	return &Session{
		Fd : fd,
		connWs : con,
		userData : nil,
	}
}

func NewSessionWithTCP(fd uint32, con *socket.ConnectionNet) *Session {
	return &Session{
		Fd : fd,
		connNet : con,
		userData : &AgentUserData{},
	}
}

func (s *Session) GetWsConn() *socket.ConnectionWs {
	return s.connWs
}

func (s *Session) GetTcpConn() *socket.ConnectionNet {
	return s.connNet
}

func (s *Session) setUserData(uid int32, account string) {
	s.userData = &AgentUserData{Uid: uid, Account: account}
	clog.LogLogic().Printf("Session 绑定用户信息,fd=%d, uid=%d, account=%s\n", s.Fd, uid, account)
}

// 设置account
func (s *Session) setAccount(account string) {
	s.userData.Account = account
	clog.LogLogic().Printf("Session 绑定用户信息Account, fd=%d, account=%s\n", s.Fd, s.userData.Account)
}

// 设置uid
func (s *Session) setUid(uid int32) {
	s.userData.Uid = uid
	clog.LogLogic().Printf("Session 绑定用户信息Uid, fd=%d, uid=%d, account=%s\n", s.Fd, uid, s.userData.Account)
}

// 设置房间服
func (s *Session) SetRoomServer(addr string) {
	s.userData.RoomServerAddr = addr
	clog.LogLogic().Printf("Session 绑定用户信息RoomServer, fd=%d, uid=%d, roomServerAddrp=%s\n", s.Fd, s.userData.Uid, s.userData.RoomServerAddr)
}

// 移除房间服
func (s *Session) RemoveRoomServer() {
	s.userData.RoomServerAddr = ""
	s.userData.RoomServerPtr = nil
	clog.LogLogic().Printf("Session 移除用户信息RoomServer, fd=%d, uid=%d\n", s.Fd, s.userData.Uid)
}

func (s *Session) GetUserData() *AgentUserData {
	return s.userData
}

func (s *Session) IsSignIn() bool {
	return !(s.userData.Uid == 0)
}
